/**
 * Created by ptt on 2016/6/8/0008.
 */
define([], function () {
    //GUID生成
    !Math.guid && (Math.guid = function () {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        }).toUpperCase();
    });

    var Model = {
        //�־û���¼
        records: {},
        inherited: function () {},
        created: function () {
            this.records = {};
            this.attributes = [];
        },
        prototype: {
            newRecord: true,
            create: function () {
                if (!this.id) this.id = Math.guid();
                this.newRecord = false;
                this.parent.records[this.id] = this;
            },
            destroy: function () {
                delete this.parent.records[this.id];
            },
            update: function () {
                this.parent.records[this.id] = this;
            },
            save: function () {
                this.newRecord ? this.create() : this.update();
            },
            init: function (attrs) {
                attrs && this.load(attrs);
            },
            load: function (attrs) {
                for (var name in attrs) {
                    attrs.hasOwnProperty(name) && (this[name] = attrs[name]);
                }
            },
            attributes: function () {
                var result = {};
                var attrs = this.parent.attributes;
                for (var key in attrs) {
                    var attr = attrs[key];
                    result[attr] = this[attr];
                }
                result.id = this.id;
                return result;
            },
            toJson: function () {
                return (this.attributes());
            }
        },
        create: function () {
            var object = Object.create(this);
            object.parent = this;
            object.prototype = object.fn = Object.create(this.prototype);
            object.created();
            object.inherited();
            return object;
        },
        init: function () {
            var instance = Object.create(this.prototype);
            instance.parent = this;
            instance.init.apply(instance, arguments);
            return instance;
        },
        find: function (id) {
            return this.records[id] || null;
        },
        extend: function (o) {
            var extended = o.extended;
            jQuery.extend(this, o);
            extended && extended(this);
        },
        include: function (o) {
            var included = o.included;
            jQuery.extend(this.prototype, o);
            included && included(this);
        },
        populate: function (values) {
            this.records = {};
            for (var key in values) {
                var record = this.init(values[key]);
                record.save();
            }
        },
        saveLocal: function (name) {
            var result = [];
            for (var key in this.records) {
                result.push(this.records[key]);
            }
            localStorage[name] = JSON.stringify(result);
        },
        loadLocal: function (name) {
            var result = JSON.parse(localStorage[name]);
            this.populate(name);
        },
        event: $({})
    };

    return Model;

});